package cz.muni.fi.pa165.languageschool.rest.client.app;

/**
 * Basic class which represents School application
 *
 * @author Marketa Trachtova (255498) marketa.tr(at)gmail.com
 */
public class SchoolApp {

    Courses course;
    Students student;
    String resourceBase;
    boolean running = true;

    /**
     * This method is used for selecting action which user wants to do
     */
    public void select() {
        printWelcome();
        resourceBase = scanResourceBase();
        student = new Students(this, resourceBase);
        course = new Courses(this, resourceBase);
        while (running) {
            Helper.clear();
            printHelp();

            Object resp = Helper.getResponse("Choose", "q", "c", "s");
            if (resp == null) {
                return;
            }
            if ("q".equals(resp)) {
                return;
            } else if ("c".equals(resp)) {
                course.select();
            } else if ("s".equals(resp)) {
                student.select();
            }

        }

    }
    
     /**
     * Gets server address from user
     *
     * @return rest server resources base
     */
    private String scanResourceBase() {
        String port = "http://localhost:8080/pa165/rest/";
        Object resp = Helper.getResponse("Please type server address (example: localhost:8080)", String.class);
        if (resp == null) {
            return port;
        } else {
            return (String) "http://" + resp + "/pa165/rest/";
        }
    }

    /**
     * printHelp() returns on standard output options which can user choose q -
     * exit s - Work with students c - Work with courses
     */
    private void printHelp() {
        System.out.println("_____");
        System.out.println("q - exit");
        System.out.println("s - Work with students");
        System.out.println("c - Work with courses");
    }

    /**
     * This method prints welcome note on standard output
     */
    private void printWelcome() {
        System.out.println("PA165 Languageschool REST api client");
    }
}
